﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.Core.Common.Web;


using Jzy.Workflow.Flow;

using Jzy.Workflow.Node;
using Jzy.Workflow.Line;
using System.Xml.Linq;
using Jzy.Workflow.Html;
using Frame.Lib.Utilities;
using Frame.Lib.Enums;
using Frame.Lib.Core.Dict;


namespace Jzy.Web.WorkFlow.Design
{
    public partial class Tab1 : PageEdit
    {
        private string FlowID
        {
            get
            {
                return Request["FlowID"];
            }
        }
        private FlowBO _flow = new FlowBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDrp();
                DispInfo();
                //if (FlowID == null)
                //{
                //    btnChange.Visible = false;
                //}

                lnkDownload.Visible = false;
                if (!string.IsNullOrEmpty(FlowID))
                {
                    if (!string.IsNullOrEmpty(_flow.GetInfo(FlowID).FormTemplate))
                    {
                        lnkDownload.NavigateUrl = "Download.aspx?FlowID=" + FlowID;
                        lnkDownload.Visible = true;
                    }
                }
            }
        }

        private void DispInfo()
        {
            if (FlowID != null && FlowID != "")
            {
                FlowInfo info = _flow.GetInfo(FlowID);
                EntityUtil.SetControl(pnlMain, info);
            }
        }

        private void BindDrp()
        {
            DictBO dict = new DictBO();

            dict.BindListControl(TypeCode, (int)DictItem.流程类型);


            WebUtil.BindListControlWithEnum(DriveMode, typeof(DriveMode));

            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Save(SaveMode.Save);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("List.aspx", true);
        }

        public void Save(SaveMode mode)
        {
            if (string.IsNullOrEmpty(FlowID) && fileForm.PostedFile.ContentLength == 0)
            {
                JSUtil.Alert("请上传表单模板");
                return;
            }

            string strName = Name.Text.Trim();
            bool blnFlag = _flow.ExistField(TableField.Name, strName, FlowID);
            if (blnFlag)
            {
                JSUtil.Alert("该流程名程已存在");
            }
            else
            {
                FlowInfo info = _flow.GetInfo(FlowID);

                EntityUtil.SetEntity(pnlMain, info);

                if (fileForm.PostedFile.ContentLength > 0)
                {
                    info.FormTemplate = HtmlAnalyzer.GetHtml(fileForm.PostedFile).ToString();
                }

                _flow.Save(info);
                

                //保存开始和结束结点
                if (FlowID == null || FlowID == "")
                {
                    FlowNodeBO node = new FlowNodeBO();

                    FlowNodeInfo begin = new FlowNodeInfo();
                    begin.Name = FlowNodeType.拟稿.ToString();
                    begin.Left = 50;
                    begin.Top = 50;
                    begin.FlowID = info.ID;
                    begin.TypeID = (int)FlowNodeType.拟稿;

                    node.Save(begin);

                    //FlowNodeInfo proposer = new FlowNodeInfo();//申请人
                    //proposer.Name = "申请人";
                    //proposer.FlowID = info.ID;
                    //proposer.TypeID = (int)FlowNodeType.常规;
                    //proposer.Left = 50;
                    //proposer.Top = 300;
                    //node.Save(proposer);

                    FlowNodeInfo end = new FlowNodeInfo();
                    end.Name = FlowNodeType.结束.ToString();
                    end.FlowID = info.ID;
                    end.Left = 400;
                    end.Top = 300;
                    end.TypeID = (int)FlowNodeType.结束;
                    node.Save(end);

                    FlowLineBO line = new FlowLineBO();

                    FlowLineInfo lineInfo = new FlowLineInfo();
                    lineInfo.Name = "";
                    lineInfo.FlowID = info.ID;
                    lineInfo.FromNodeID = begin.ID;
                    lineInfo.ToNodeID = end.ID;
                    line.Save(lineInfo);

                    //lineInfo = new FlowLineInfo();
                    //lineInfo.Name = "";
                    //lineInfo.FlowID = info.ID;
                    //lineInfo.FromNodeID = proposer.ID;
                    //lineInfo.ToNodeID = end.ID;                    
                    //line.Save(lineInfo);
                }

                //保存模板

                Response.Redirect("Tab2.aspx?FlowID=" + info.ID);
            }
        }       
    }
}
